package com.healthbox.appointmentservice.controller;

import com.healthbox.appointmentservice.DTO.AppointmentLimitDTO;
import com.healthbox.appointmentservice.DVO.AppointmentLimitDVO;
import com.healthbox.appointmentservice.common.Result;
import com.healthbox.appointmentservice.entity.Hospital;
import com.healthbox.appointmentservice.service.AppointmentLimitModifyService;
import com.healthbox.appointmentservice.service.AppointmentLimitQueryService;
import com.healthbox.appointmentservice.service.HospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * @author pengpeng
 * @date 2022/5/8
 */
@RestController
@RequestMapping(path = "/appointments/limit")
@Slf4j
@Api(tags = "检测名额限制")
public class AppointmentLimitController {

    @Autowired
    private AppointmentLimitModifyService limitModifyService;
    @Autowired
    private AppointmentLimitQueryService limitQueryService;

    @Autowired
    private HospitalService hospitalService;

    @ApiOperation( value = "添加检测名额限制", notes = "即发布一个可供预约的时间段以及人数")
    @PostMapping
    public Result<AppointmentLimitDVO> newAppointmentLimit(
            @RequestBody AppointmentLimitDTO appointmentLimitDTO,
            @RequestHeader(value = "user-phone") @NotNull String userPhone
    ) {
        log.info(userPhone + "添加了一条新的可预约记录" + appointmentLimitDTO);
        Hospital hospital = hospitalService.findOne(userPhone);
        if (hospital == null ) {
            log.error("用户不是机构管理员");
            return Result.badRequest("该用户不是核酸机构管理员");
        }
        if (!Objects.equals(hospital.getHospitalId(), appointmentLimitDTO.getHospitalId())) {
            log.error("用户不是指定机构的管理员");
            return Result.noAuthorization("你只能管理自己核酸检测机构");
        }
        AppointmentLimitDVO vo = limitModifyService.newAppointmentLimit(appointmentLimitDTO);
        return vo==null? Result.badRequest() : Result.ok(vo);
    }

    @ApiOperation( value = "查询指定编号的检测限制详情", notes = "查询检测名额限制信息，包括该名额限制已预约人数")
    @ApiImplicitParam(name = "id", value = "检测机构唯一标识符", paramType = "path", dataType = "long", example = "1")
    @GetMapping("/{id}")
    public Result<AppointmentLimitDVO> appointmentLimitDetail(@PathVariable Long id ) {
        AppointmentLimitDVO vo = limitQueryService.findCurrentInfoById(id);
        return vo==null? Result.badRequest() : Result.ok(vo);
    }

    @ApiOperation( value = "获取指定机构的检测名额限制", notes = "查询检测名额限制信息，同时可以利用子查询查出该名额限制已预约人数")
    @ApiImplicitParam(name = "hospitalId", value = "检测机构唯一标识符", dataType = "long", example = "1")
    @GetMapping
    public Result<List<AppointmentLimitDVO>> appointmentLimitList(
            @RequestParam Long hospitalId,
            @RequestHeader(value = "user-phone") @NotNull String userPhone
            ){
        log.info("手机号为" + userPhone + "用户查看了检测机构id=" + hospitalId + "的可预约时间段信息");
        List<AppointmentLimitDVO> list = limitQueryService.findAppointmentLimits(null, null, hospitalId);
        return list==null? Result.badRequest() : Result.ok(list);
    }

    @ApiOperation(value = "删除检测名额限制记录", notes = "删除检测名额限制记录时必须保证该限制记录没有已预约，否则不能删除")
    @ApiImplicitParam(name = "id", value = "检测名额限制唯一标识符", paramType = "path", example = "1")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteAppointmentLimit(
            @PathVariable Long id,
            @RequestHeader(value = "user-phone") @NotNull String userPhone) {
        log.info("删除一个可预约记录");
        Hospital hospital = hospitalService.findOne(userPhone);
        if (hospital == null ) {
            log.error("用户不是机构管理员");
            return Result.badRequest("该用户不是核酸机构管理员");
        }
        Boolean success = limitModifyService.deleteAppointmentLimit(userPhone, id);
        return  success? Result.ok(true) : Result.badRequest("删除失败");
    }

//    @ApiOperation(value = "修改检测名额限制记录", notes = "修改检测名额限制时必须保证修改后的最大名额数必须大于当前已预约人数，否则修改失败")
//    @PutMapping()
//    public Result<AppointmentLimitDVO> updateAppointmentLimit(@RequestBody AppointmentLimitDTO appointmentLimitDTO) {
//        AppointmentLimitDVO vo = limitModifyService.updateAppointmentLimit(appointmentLimitDTO);
//        return  vo == null? Result.badRequest() : Result.ok(vo);
//    }


}
